package com.cmf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmf.dto.CategoryDto;
import com.cmf.entity.Category;
import com.cmf.mapper.CategoryMapper;
import com.cmf.service.CategoryService;
import com.cmf.common.PageResult;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

/**
 * @Author: cmf
 * @Date: 2025/8/26 17:32
 * @Version: v1.0.0
 * @Description: TODO
 **/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    /**
     * 分页查询分类信息
     * @param categoryDto 分类查询条件，包括页码、每页大小、分类类型和分类名称
     * @return PageResult<Category> 分页结果，包含总记录数和当前页的分类数据
     */
    @Override
    public PageResult<Category> pageInfo( CategoryDto categoryDto) {
        // 构造分页对象
        Page<Category> page = new Page<Category>(categoryDto.getCurrentPage()!=null?categoryDto.getCurrentPage():1, categoryDto.getPageSize()!=null?categoryDto.getPageSize():10);
        // 构造查询条件
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper
                .eq( categoryDto.getType()!=null, Category::getType, categoryDto.getType())
                .like(categoryDto.getName()!=null, Category::getName, categoryDto.getName())
                .orderByDesc(Category::getSort);
        // 执行分页查询
        Page<Category> categoryPage = this.getBaseMapper().selectPage(page, categoryLambdaQueryWrapper);
        // 封装分页结果
        PageResult<Category> pageResult = new PageResult<>(categoryPage.getTotal(), categoryPage.getRecords());
        return pageResult;
    }
}
